package com.campus.client.view;

import com.campus.common.vo.User;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class AccountManagementFrame {

    private Button backButton;
    private Button studentButton;
    private Button teacherButton;
    private Button adminButton;

    private User user=new User();
    public User getUser() {return user;}
    public void setUser(String Id,String password,String role) {
        this.user.setId(Id);
        this.user.setPassword(password);
        this.user.setRole(role);
        //System.out.print("用户名"+this.user.getId());
    }

    public void start(Stage primaryStage) {
        // 创建主VBox容器
        VBox mainVBox = new VBox();
        mainVBox.setPrefHeight(400.0);
        mainVBox.setPrefWidth(640.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(-1.0);
        anchorPane.setPrefWidth(-1.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.DODGERBLUE);
        topRect.setHeight(89.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(640.0);

        // 创建标题文本
        Text titleText = new Text("账号管理");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(232.0);
        titleText.setLayoutY(58.0);
        titleText.setStrokeWidth(0.0);
        titleText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        titleText.setWrappingWidth(176.66668701171875);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 36.0));

        // 创建内容VBox
        VBox contentVBox = new VBox();
        contentVBox.setAlignment(Pos.CENTER);
        contentVBox.setLayoutX(148.0);
        contentVBox.setLayoutY(100.0);
        contentVBox.setPrefHeight(227.0);
        contentVBox.setPrefWidth(344.0);
        contentVBox.setSpacing(15.0);

        // 创建提示文本
        Text promptText = new Text("选择管理账号类型");
        promptText.setStrokeWidth(0.0);
        promptText.setFont(Font.font(24.0));

        // 创建学生账号按钮
        studentButton = new Button("学生账号");
        studentButton.setMnemonicParsing(false);
        studentButton.setPrefHeight(41.0);
        studentButton.setPrefWidth(215.0);
        studentButton.setStyle("-fx-background-color: #1E90FF;");
        studentButton.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        studentButton.setTextFill(Color.WHITE);
        studentButton.setFont(Font.font(18.0));
        studentButton.setOnAction(e -> {
            try {
                showStudentAccountManagementFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        // 创建教师账号按钮
        teacherButton = new Button("教师账号");
        teacherButton.setMnemonicParsing(false);
        teacherButton.setPrefHeight(41.0);
        teacherButton.setPrefWidth(215.0);
        teacherButton.setStyle("-fx-background-color: #1E90FF;");
        teacherButton.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        teacherButton.setTextFill(Color.WHITE);
        teacherButton.setFont(Font.font(18.0));
        teacherButton.setOnAction(e -> {
            try {
                showTeacherAccountManagementFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        // 创建管理员账号按钮
        adminButton = new Button("管理员账号");
        adminButton.setMnemonicParsing(false);
        adminButton.setPrefHeight(41.0);
        adminButton.setPrefWidth(215.0);
        adminButton.setStyle("-fx-background-color: #1E90FF;");
        adminButton.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        adminButton.setTextFill(Color.WHITE);
        adminButton.setFont(Font.font(18.0));
        adminButton.setOnAction(e -> {
            try {
                showAdminAccountManagementFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        // 将组件添加到内容VBox
        contentVBox.getChildren().addAll(promptText, studentButton, teacherButton, adminButton);

        // 创建返回按钮
        backButton = new Button("返回");
        backButton.setLayoutX(552.0);
        backButton.setLayoutY(354.0);
        backButton.setMnemonicParsing(false);
        backButton.setPrefHeight(32.0);
        backButton.setPrefWidth(73.0);
        backButton.setOnAction(e -> {
            try {
                back();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(topRect, titleText, contentVBox, backButton);

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("账号管理");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void back() throws IOException {
        // 获取当前登录窗口的Stage
        Stage accountManagementStage = (Stage) backButton.getScene().getWindow();

        AdminMainFrame adminMainFrame = new AdminMainFrame();
        adminMainFrame.setUser(user.getId(),user.getPassword(),user.getRole());

        Stage stage = new Stage();
        adminMainFrame.show(stage);

        accountManagementStage.close();
    }

    public void showStudentAccountManagementFrame() throws Exception {
        Stage registerStage = (Stage) studentButton.getScene().getWindow();
        StudentAccountManagementFrame accountManagementFrame = new StudentAccountManagementFrame();
        accountManagementFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage accountManagementStage = new Stage();

        accountManagementFrame.start(accountManagementStage);
        registerStage.close();
    }

    public void showTeacherAccountManagementFrame() throws Exception {
        Stage registerStage = (Stage) teacherButton.getScene().getWindow();
        TeacherAccountManagementFrame accountManagementFrame = new TeacherAccountManagementFrame();
        accountManagementFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage accountManagementStage = new Stage();

        accountManagementFrame.start(accountManagementStage);
        registerStage.close();
    }

    public void showAdminAccountManagementFrame() throws Exception {
        Stage registerStage = (Stage) adminButton.getScene().getWindow();
        AdminAccountManagementFrame accountManagementFrame = new AdminAccountManagementFrame();
        accountManagementFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage accountManagementStage = new Stage();

        accountManagementFrame.start(accountManagementStage);
        registerStage.close();
    }

}